//
//  ZXXTextView.swift
//  M-Weibo
//
//  Created by ZXX on 16/4/23.
//  Copyright © 2016年 ZXX. All rights reserved.
//

import UIKit

class ZXXTextView: UITextView {
//    设置placehoder
    var placehoder: String?{
        didSet{
            placehoderLabel.text = placehoder
        }
    }
//    设置place的字体大小和textView的字体大小相同
     override var font: UIFont? {
        didSet{
        placehoderLabel.font = font
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
//        设置UI
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  MARK: -  设置UI
    private func setupUI(){
//        加载控件
        addSubview(placehoderLabel)
//        添加约束
        placehoderLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(8)
            make.leading.equalTo(5)
            make.width.equalTo(SCREENWIDTH - 10)
            //    //  MARK: -  监听textView 显示 隐藏placehoder
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ZXXTextView.textViewTextDidChange), name: UITextViewTextDidChangeNotification, object: nil)
        }
    }
    
//  //  MARK: -  监听textView 显示 隐藏placehoder
    @objc private func textViewTextDidChange(){
        placehoderLabel.hidden = self.hasText()
    }

//    //  MARK: -  移除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    

   //  MARK: -  懒加载控件 显示placeHoderLabel
    private lazy var placehoderLabel: UILabel = {
        let place = UILabel()
        place.textColor = UIColor.lightGrayColor()
        place.numberOfLines = 0
        return place
    }()
    
}
